package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import model.Command;
import utilities.CRes;
import utilities.GameResource;
import font.BitmapFont;

public class MsgDlg extends Dialog{
	protected String[] info;	
	public Vector list = new Vector();
	
	public int index = 0, m_width, m_height, m_left, m_top;
	public boolean isWaiting = false;
	int size = 0;	
	int deltaX = 2;
	
	public void show() {
		GameCanvas.currentDialog = this;
	}
	public void setInfo(String info, Command center, Vector list) {
		init(info);		
		
		this.center = null;
		this.center = center;			
		index = 0;
		if(list != null){
			CRes.cleanVector(this.list);
			this.list = list;
			Command cmd = (Command) list.elementAt(index);
			center.action = cmd.action;
		}else{
			this.list.removeAllElements();
		}
		size=this.list.size();
	}
	public void setInfo(String info, Command center, Vector list, byte position) {
		init(info);
		
		if(position == 1){
			m_left = GameCanvas.hw - m_width / 2;
			m_top = GameCanvas.h - GameCanvas.hBottomBar - m_height;
		}
		else{
			m_left = GameCanvas.hw - m_width / 2;
			m_top = GameCanvas.hh - m_height / 2;		
		}
		
		this.center = null;
		this.center = center;			
		index = 0;
		if(list != null){
			CRes.cleanVector(this.list);
			this.list = list;
			Command cmd = (Command) list.elementAt(index);
			center.action = cmd.action;
		}else{
			this.list.removeAllElements();
		}
		size=this.list.size();
	}
	
	public void init(String info){
		m_width = GameCanvas.w3d4;
		
		String[] subInfo = BitmapFont.splitString(info, "\n");
		Vector vtTmp = new Vector();
		int i, j;
		for(i = 0; i < subInfo.length; i++){
			String[] tmp = BitmapFont.m_bmFont.splitFontBStrInLine(subInfo[i], m_width - 16);
			for(j = 0; j < tmp.length; j++)
				vtTmp.addElement(tmp[j]);
			tmp = null;
		}
		
		this.info = new String[vtTmp.size()];
		for(i = 0; i < vtTmp.size(); i++)
			this.info[i] = (String)vtTmp.elementAt(i);
		
		subInfo = null;
		CRes.cleanVector(vtTmp);
		
		m_height = this.info.length * 15 + 30;
		
		if(m_height < 55)
			m_height = 55;
		m_left = GameCanvas.hw - m_width / 2;
		m_top = GameCanvas.hh - m_height / 2;
	}
	public void paint(Graphics g){
		int tmp = 0;
		
		PaintPopup.paintRoundRect(g, m_left, m_top, m_width, m_height, 0x9be5ff);		
		
		if (isWaiting) {
			GameResource.instance.m_frameWaiting.drawSeriFrame(GameCanvas.hw - 10, m_top + m_height - 30, Graphics.HCENTER | Graphics.VCENTER, g);
		}	
		
		tmp = m_top + m_height - 20;
		if (size > 0) {
			Command cmd = (Command) list.elementAt(index);
			
			BitmapFont.drawBoldFont(g, cmd.caption, GameCanvas.hw, tmp, 0x3a001e, Graphics.HCENTER);
			if(size > 1){
				int a = BitmapFont.m_bmFont.stringWidth(cmd.caption);
				PaintPopup.paintArrow(g,0, GameCanvas.hw - a / 2 - 15, tmp, 30 + a, deltaX);
				deltaX = 2 - deltaX;
			}
		}else{
			if (center != null)
				BitmapFont.drawBoldFont(g, center.caption, GameCanvas.hw, tmp, 0x3a001e, Graphics.HCENTER);
		}		
		
		tmp = m_top + 5;
        int a = info.length;
		for (int i = 0; i < a; i++) {
			if(info[i].length() > 0){
				BitmapFont.drawNormalFont(g, info[i], GameCanvas.hw , tmp, 0x3a001e, Graphics.HCENTER);
				tmp += 15;
			}
		}
	}
	
	private void setIndex(int in){
		if (size > 0) {
			index+=in;
			if(index<0)
				index=size-1;
			if(index>=size)
				index=0;
			Command cmd = (Command) list.elementAt(index);
			center.action = cmd.action;
		}
	}
	
	public void update() {
		if (GameCanvas.keyPressed[4]) {
			GameCanvas.keyPressed[4] = false;
			setIndex(-1);
		} else if (GameCanvas.keyPressed[6]) {
			GameCanvas.keyPressed[6] = false;
			setIndex(1);
		}
		if(GameCanvas.isPointerClick){
			int ww=0;
			if(list!=null&&list.size()>0){
				Command cmd = (Command) list.elementAt(index);
				ww = BitmapFont.m_bmFont.stringWidth(cmd.caption)+17;
			}else if(center!=null){
				ww = BitmapFont.m_bmFont.stringWidth(center.caption)+17;
			}
			
			int tmp = m_top + m_height - 20;
			if(center!=null&&GameCanvas.isPointer(GameCanvas.hw-ww/2, tmp, ww, 20)){
				center.action.perform();
			}else if(GameCanvas.isPointer(m_left, tmp, m_width, 20)){
				int px=GameCanvas.hw-GameCanvas.px;
				if(px>ww/2){
					setIndex(-1);
				}else if(px<-ww/2)
					setIndex(1);
			}
		}
		super.update();
	}
}
